﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Zo.ExcelBox.Base
{
    /// <summary>
    /// 对时间转化的扩展
    /// </summary>
    public static class ToDateTimeExtension
    {


        /// <summary>
        /// Unix时间戳(微信)转化成C#时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳</param>
        /// <returns></returns>
        public static DateTime UnixToDateTime(this long timeStamp)
        {
            DateTimeOffset utcTime = DateTimeOffset.FromUnixTimeMilliseconds(timeStamp);
            return utcTime.ToLocalTime().DateTime;
        }

        /// <summary>
        /// Unix时间戳(微信)转化成C#时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳</param>
        /// <returns></returns>
        public static DateTime UnixToDateTime(this string timeStamp)
        {
            return UnixToDateTime(long.Parse(timeStamp));
        }

        public static DateTime StringToData(this string unixTimestampStr)
        {
            DateTime dateTime = DateTime.ParseExact(
    unixTimestampStr,
    "yyyyMMddHHmmss",
    System.Globalization.CultureInfo.InvariantCulture
);

            return dateTime;
        }


        /// <summary>
        /// C#时间转换成Unix时间戳(微信)
        /// </summary>
        /// <param name="dateTime">时间</param>
        /// <returns></returns>
        public static long ToUnixTimeStamp(this DateTime dateTime)
        {
            DateTimeOffset dto = new DateTimeOffset(dateTime);
            return dto.ToUnixTimeMilliseconds();
        }
    }
}
